#include <iostream>
#include <string>
#include <stack>
#include <cstdio>
using namespace std;

void del(string &x, unsigned y) {
	x = x.substr(0, y) + x.substr(y + 1, x.size() - y);
}

int main() {
	int x;
	cin >> x;
	scanf("\n");
	while (x--) {
		string x;
		getline(cin, x);
		for (unsigned i = 0; i < x.size(); ++i) {
			if (x[i] == ' ') {
				del(x, i);
				--i;
			}
		}
		//cout << x << endl;
		stack<int> s;
		for (unsigned i = 0; i < x.size(); ++i) {
			//cout << x[i] << ' ' << i << endl;
			if (x[i] == '(') {
				//cout << i << endl;
				s.push(i);
			} else if (x[i] == ')') {
				int a = s.top();
				s.pop();
				//cout << i << " " << a << endl;
				if (a == 0 || x[a-1] == '+' || x[a-1] == '(' || i - a <= 2) {
					del(x, i);
					del(x, a);
					i -= 2;
				}
				//cout << x << endl;
			}
		}
		cout << x << "\n";
	}
	return 0;
}
